﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using GameRobot;
using Microsoft.Xna.Framework.Input;
using RobotGameData;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameRobotControl
{
    public class SampleRobot : Robot
    {
        List<string> listMethod = new List<string>();

        public SampleRobot(string assetName, Vector2 startLocation, Vector2 frameSize, int framesPerDirection)
        {
            this.AssetName = assetName;
            _robotBridge = new RobotBridge();
            _robotAction = new SampleManualRobotAction(_robotBridge);
            _controlType = ControlType.ManualControl;


            Position = startLocation;
            FrameSize = frameSize;
            Origin = frameSize / 2;
            FrameLength = 0.05f;
            MaxFramesPerDirection = framesPerDirection;
            Direction = Direction.South;
            Speed = 5f;
          
        }
        public SampleRobot()
        {
            _robotBridge = new RobotBridge();
            _robotAction = new SampleManualRobotAction(_robotBridge);
            _controlType = ControlType.ManualControl;
        }

        public override void HandleInput(InputState input)
        {
            Vector2 playerMovement = Vector2.Zero;

            if (input.IsNewKeyPress(Keys.D))
            {
                playerMovement.X = -1;
            }
            else if (input.IsNewKeyPress(Keys.A))
            {
                playerMovement.X = 1;
            }
            if (input.IsNewKeyPress(Keys.W))
            {
                playerMovement.Y = -1;
            }
            else if (input.IsNewKeyPress(Keys.S))
            {
                playerMovement.Y = 1;
            }

            if (playerMovement != Vector2.Zero)
            {
                playerMovement.Normalize();
            }


            if (playerMovement == Vector2.Zero)
                IsAnimating = false;
            else
                IsAnimating = true;

            if (playerMovement.X > 0 && playerMovement.Y == 0) Direction = Direction.East;
            if (playerMovement.X < 0 && playerMovement.Y == 0) Direction = Direction.West;
            if (playerMovement.X == 0 && playerMovement.Y > 0) Direction = Direction.South;
            if (playerMovement.X == 0 && playerMovement.Y < 0) Direction = Direction.North;
            if (playerMovement.X < 0 && playerMovement.Y < 0) Direction = Direction.NorthWest;
            if (playerMovement.X < 0 && playerMovement.Y > 0) Direction = Direction.SouthWest;
            if (playerMovement.X > 0 && playerMovement.Y < 0) Direction = Direction.NorthEast;
            if (playerMovement.X > 0 && playerMovement.Y > 0) Direction = Direction.SouthEast;

            Position += Speed * playerMovement;

            LastMovement = Speed * playerMovement;
        }

        public override void Update(GameInput gameinput)
        {
            ParseMessage(gameinput.game_message);

            _robotAction.Action(gameinput);

            GameMessage action = null;
            action = PopMessage();
            if (action != null)
            {
                ParseAction(action);
            }

            DoAction();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        #region parse message
        public override void ParseMessage(GameMessage message)
        {
            switch (message.message)
            {
                case messagedef.ChangeToManualControl:
                    if (_controlType != ControlType.ManualControl)
                    {
                        _controlType = ControlType.ManualControl;
                        _robotBridge.Reset();
                        _robotAction = new SampleManualRobotAction(_robotBridge);
                    }
                    break;
                case messagedef.ChangeToChedulingControl:
                    if (_controlType != ControlType.SchedulingControl)
                    {
                        _controlType = ControlType.SchedulingControl;
                        _robotBridge.Reset();
                        _robotAction = new SampleSchedulingRobotAction(_robotBridge);
                    }
                    break;
            }
        }
        #endregion

        #region parse action
        public override void ParseAction(GameMessage action)
        {
            switch (action.message)
            {
                case messagedef.MoveAhead:
                    {
                        float range = (float)(action.param1);
                        MoveAhead(range);
                    }
                    break;
                case messagedef.MoveHead:
                    {
                        MoveHead();
                    }
                    break;
                case messagedef.MoveLeft:
                    {
                        MoveLeft();
                    }
                    break;
                case messagedef.MoveBack:
                    {
                        MoveBack();
                    }
                    break;
                case messagedef.MoveRight:
                    {
                        MoveRight();
                    }
                    break;
                //case messagedef.StopTurn:
                //    {
                //        StopTurn();
                //    }
                //    break;
                //case messagedef.StopAllAction:
                //    {
                //        StopAllAction();
                //    }
                //    break;
                //case messagedef.GetPosition:
                //    {
                //        GameMessage message = new GameMessage();
                //        message.message = messagedef.GetPosition;
                //        message.param1 = pos.X;
                //        message.param2 = pos.Y;
                //        PushMessage(message, false);
                //    }
                //    break;
            }
        }
        #endregion

        #region action of robot

        public void Move(Point start, Point end)
        {
            
        }
        public void MoveAhead(float range)
        {
            Vector2 playerMovement = new Vector2(0, range);
            Position += Speed * playerMovement;
            LastMovement = Speed * playerMovement;
            moving = true;
        }

        public void MoveHead()
        {
            Vector2 playerMovement = new Vector2(0, 1);
            Position += Speed * playerMovement;
            LastMovement = Speed * playerMovement;
            moving = true;
        }

        public void MoveBack()
        {
            Vector2 playerMovement = new Vector2(0, -1);
            Position += Speed*playerMovement;
            LastMovement = Speed * playerMovement;
            moving = true;
        }

        public void MoveLeft()
        {
            Vector2 playerMovement = new Vector2(-1, 0);
            Position += Speed * playerMovement;
            LastMovement = Speed * playerMovement;
            moving = true;
        }

        public void MoveRight()
        {
            Vector2 playerMovement = new Vector2(1, 0);
            Position += Speed * playerMovement;
            LastMovement = Speed * playerMovement;
            moving = true;
        }

        public void Move()
        {
            StopMove();
        }

        public void StopMove()
        {
            ResumeControler();
            moving = false;
        }
        
        #endregion

        #region do action
        public void DoAction()
        {
            if (moving)
            {
                Move();
            }
        }
        

        #endregion
    }
}
